import { ItemCategory } from '../shared';

export interface StackableItem {
  id: string;
  category: ItemCategory;
  quantity: number;
  maxStack: number;
}

export class Stacks {
  static canStack(a: StackableItem, b: StackableItem): boolean {
    return a.id === b.id && a.category === b.category;
  }

  static merge(target: StackableItem, source: StackableItem): number {
    if (!Stacks.canStack(target, source)) {
      return source.quantity;
    }

    const space = target.maxStack - target.quantity;
    const transfer = Math.min(space, source.quantity);
    target.quantity += transfer;
    source.quantity -= transfer;
    return source.quantity;
  }
}


